package com.example.cephdemo.demos.base;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * @author winterchen
 * @version 1.0
 * @date 2022/4/11 15:37
 * @description 统一返回类
 **/
@Data
public class Result implements Serializable {

    private Integer code;

    private Object data;

    private String message;


    public static Result success() {
        Result result = new Result();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    public static Result success(Object data) {
        Result result = success();
        result.setData(data);
        return result;
    }

    public static Result error() {
        Result result = new Result();
        result.setCode(ResultCode.KNOWN_ERROR.getCode());
        result.setMessage(ResultCode.KNOWN_ERROR.getMessage());
        return result;
    }

    public static Result error(Integer code) {
        final ResultCode resultCode = ResultCode.getByCode(code);
        Result result = new Result();
        result.setCode(result.getCode());
        result.setMessage(result.getMessage());
        return result;
    }

    public static Result error(Integer code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

}